package com.lanou3g.springcloud.hello.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RefreshScope
@Slf4j
@RestController
public class HelloController {

	@Value("${student.sname}")
	private String sname;

	@Autowired
	private Registration registration;

	@RequestMapping("/")
	public String defaultHello() {

		// 模拟耗时操作
		/*int num = new Random().nextInt(10000);
		log.info("休眠：" + num +"ms.");
		try {
			Thread.sleep(num);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}*/

		int port = registration.getPort();
		return "Hello "+this.sname+"!, Port: " + port;
	}

	@RequestMapping("/name/{name}")
	public String helloName(@PathVariable String name) {
//		int num = 8 / 0;
		return "Hello, " + name;
	}

	@RequestMapping("/json/{name}")
	public String helloJson(@PathVariable String name) {
		return "{\"server\":\"helloJson\", \"name\":\"" + name + "\"}";
	}
}
